using System.Collections;
using UnityEngine;

/// <summary>
/// 阳光生产组件 - 用于向日葵等植物生产阳光
/// </summary>
[DisallowMultipleComponent]
public class SunProductionComponent : MonoBehaviour
{
    [Header("阳光生产配置")]
    [SerializeField] private float m_ProductionInterval = 7.5f; // 生产间隔（秒）
    [SerializeField] private int m_SunValue = 25; // 每次生产的阳光值
    [SerializeField] private Transform m_SunSpawnPoint; // 阳光生成点
    
    [Header("视觉效果")]
    [SerializeField] private GameObject m_SunPrefab; // 阳光预制体
    [SerializeField] private Vector3 m_SunSpawnOffset = Vector3.up; // 生成偏移
    
    private bool m_IsProducing = false;
    private Coroutine m_ProductionCoroutine;
    
    /// <summary>
    /// 是否正在生产阳光
    /// </summary>
    public bool IsProducing => m_IsProducing;
    
    /// <summary>
    /// 生产间隔
    /// </summary>
    public float ProductionInterval 
    { 
        get => m_ProductionInterval; 
        set => m_ProductionInterval = Mathf.Max(0.1f, value); 
    }
    
    /// <summary>
    /// 阳光价值
    /// </summary>
    public int SunValue 
    { 
        get => m_SunValue; 
        set => m_SunValue = Mathf.Max(1, value); 
    }
    
    private void Awake()
    {
        // 如果没有设置生成点，使用自身位置
        if (m_SunSpawnPoint == null)
        {
            m_SunSpawnPoint = transform;
        }
    }
    
    /// <summary>
    /// 初始化阳光生产组件
    /// </summary>
    public void Initialize()
    {
        Logger.Log($"{name} 阳光生产组件初始化 - 间隔: {m_ProductionInterval}s, 价值: {m_SunValue}");
        StartProduction();
    }
    
    /// <summary>
    /// 开始生产阳光
    /// </summary>
    public void StartProduction()
    {
        if (m_IsProducing) return;
        
        m_IsProducing = true;
        m_ProductionCoroutine = StartCoroutine(ProductionLoop());
        Logger.Log($"{name} 开始生产阳光");
    }
    
    /// <summary>
    /// 停止生产阳光
    /// </summary>
    public void StopProduction()
    {
        if (!m_IsProducing) return;
        
        m_IsProducing = false;
        if (m_ProductionCoroutine != null)
        {
            StopCoroutine(m_ProductionCoroutine);
            m_ProductionCoroutine = null;
        }
        Logger.Log($"{name} 停止生产阳光");
    }
    
    /// <summary>
    /// 生产循环协程
    /// </summary>
    private IEnumerator ProductionLoop()
    {
        while (m_IsProducing)
        {
            yield return new WaitForSeconds(m_ProductionInterval);
            
            if (m_IsProducing)
            {
                ProduceSun();
            }
        }
    }
    
    /// <summary>
    /// 生产一个阳光
    /// </summary>
    private void ProduceSun()
    {
        // 计算生成位置
        Vector3 spawnPosition = m_SunSpawnPoint.position + m_SunSpawnOffset;
        
        // 生成阳光对象
        if (m_SunPrefab != null)
        {
            GameObject sunObject = Instantiate(m_SunPrefab, spawnPosition, Quaternion.identity);
            
            // 设置阳光值 - 使用原有Sun类的Initialize方法
            var sunComponent = sunObject.GetComponent<Sun>();
            if (sunComponent != null)
            {
                sunComponent.Initialize(m_SunValue);
                sunComponent.EnableCollection();
            }
            
            Logger.Log($"{name} 生产了价值 {m_SunValue} 的阳光");
        }
        else
        {
            // 如果没有预制体，直接通过事件系统通知
            var args = ReferencePool.Acquire<SunProducedEventArgs>();
            args.Sun = null; // 没有实际阳光对象
            args.Position = spawnPosition;
            EventHelper.Fire(this, args);
            Logger.Log($"{name} 生产了价值 {m_SunValue} 的阳光 (通过事件)");
        }
    }
    
    /// <summary>
    /// 设置生产参数
    /// </summary>
    public void SetProductionParams(float interval, int sunValue)
    {
        m_ProductionInterval = Mathf.Max(0.1f, interval);
        m_SunValue = Mathf.Max(1, sunValue);
        Logger.Log($"{name} 阳光生产参数更新 - 间隔: {m_ProductionInterval}s, 价值: {m_SunValue}");
    }
    
    /// <summary>
    /// 立即生产一个阳光（用于测试或特殊技能）
    /// </summary>
    public void ProduceSunImmediately()
    {
        ProduceSun();
    }
    
    private void OnDestroy()
    {
        StopProduction();
    }
    
    private void OnDisable()
    {
        StopProduction();
    }
    
    private void OnEnable()
    {
        if (m_IsProducing)
        {
            StartProduction();
        }
    }
    
    #if UNITY_EDITOR
    /// <summary>
    /// 在编辑器中显示生产状态
    /// </summary>
    private void OnDrawGizmosSelected()
    {
        if (m_SunSpawnPoint != null)
        {
            Gizmos.color = Color.yellow;
            Gizmos.DrawWireSphere(m_SunSpawnPoint.position + m_SunSpawnOffset, 0.5f);
        }
    }
    #endif
}
